# -*- coding:utf-8 -*-

__author__='zhaoxp'

'''
descriptor:

descr.__get__(self, obj, type=None) --> value
descr.__set__(self, obj, value) --> None
descr.__delete__(self, obj) --> None

v = t.a   <---->   v = __get__(a, t)
t.a = v   <----->  __set__(a, t, v)
del t.a   <----->  __delete__(a, t)

只有new-style objects或class的属性在被引用时,descriptor的特性才能起作用
从 object 派生的类就是 new-style class
class T(object):
    pass
 
那么这大概是怎么回事呢?
是因为object有__getattribute__属性, 这个属性的实现确保了descriptor机制
所以如果我们重写了__getattribute__, 那么就可以消除descriptor机制

http://blog.csdn.net/huithe/article/details/7484606
http://www.cnblogs.com/livingintruth/p/3601861.html
'''


class Descriptor(object):
    def __get__(self,obj,type=None):
        return '__get__ %s %s %s'%(self,obj,type)

    def __set__(self,obj,val):
        print('__set__ %s %s %s'%(self,obj,val))

    def __delete__(self,obj):
        print('__delete__ %s %s'%(self,obj))


class Foo(object):
    value = Descriptor()

    def __init__(self):
        self.data = Descriptor()


if __name__=='__main__':
    print('descriptor usage')
    foo = Foo()
    print('*'*30)
    print('foo.value = %s'%foo.value)
    print('*'*30)
    foo.value=10
    print('*'*30)
    print('foo.value = %s'%foo.value)
    print('*'*30)
    del foo.value
    print('*'*50)
    print('foo.data = %s'%foo.data)
    foo.data=100
    print('foo.data = %s'%foo.data)
    del foo.data

    